<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Emodel;
use app\common\Excel;
use think\Db;
//--学生各题型得分
class ClazzAvgScore extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$grade = input("grade");
		$examId = input("examId");
		$subject = input("subject");
		$field = input("field");
		$page = input("page");
		$rows = input("rows");
		if(!$examId) return [];
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$fields = "";
		$fileds2 = "";
		if($field) {
			$array = explode(",", $field);
			foreach ($array as $key => $value) {
				$array1 = explode(":", $value);
				$fields .= ",convert(varchar,round(".("题型".($key+1)).",1)) as '".$array1[0]."'";
				$fileds2 .= ",convert(varchar,round(".("题型".($key+1))."/".(intval($array1[1]) == 0 ? 1 : $array1[1])."*100,1))+'%' as '".$array1[0]."'";
			}
		}
		$maxScore = 120;
		if(strpos("物理,化学,政史",$subject)){
			$maxScore = 100;
		}elseif (strpos("政治,历史",$subject)) {
			$maxScore = 50;
		}elseif (strpos("体育",$subject)) {
			$maxScore = 25;
		}
		$sql = "select 实验班,班号,'平均分' as 名称,学科,convert(varchar,总分) as 总分".$fields."
		from 各题得分".$grade." where 学生ID=0 and 考试ID=".$examId." and 学科='".$subject."'
		UNION 
		select 实验班,班号,'得分率',学科,convert(varchar,round(总分/".$maxScore."*100,1))+'%'".$fileds2."
		from 各题得分".$grade." where 学生ID=0 and 考试ID=".$examId." and 学科='".$subject."'
		order by 实验班,班号,名称 desc";
		$result = Db::query($sql);
		return selectResult($result);
	}

	public function exportExcel() {
		$grade = input("grade");
		$examId = input("examId");
		$subject = input("subject");
		$field = input("field");
		$page = input("page");
		$rows = input("rows");
		if(!$examId) return [];
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$header = "实验班,班号,名称,学科,总分";
		$fields = "";
		$fileds2 = "";
		if($field) {
			$array = explode(",", $field);
			foreach ($array as $key => $value) {
				$array1 = explode(":", $value);
				$header .= ",".$array1[0];
				$fields .= ",convert(varchar,round(".("题型".($key+1)).",1)) as '".$array1[0]."'";
				$fileds2 .= ",convert(varchar,round(".("题型".($key+1))."/".(intval($array1[1]) == 0 ? 1 : $array1[1])."*100,1))+'%' as '".$array1[0]."'";
			}
		}
		$maxScore = 120;
		if(strpos("物理,化学,政史",$subject)){
			$maxScore = 100;
		}elseif (strpos("政治,历史",$subject)) {
			$maxScore = 50;
		}elseif (strpos("体育",$subject)) {
			$maxScore = 25;
		}
		$sql = "select 实验班,班号,'平均分' as 名称,学科,convert(varchar,总分) as 总分".$fields."
		from 各题得分".$grade." where 学生ID=0 and 考试ID=".$examId." and 学科='".$subject."'
		UNION 
		select 实验班,班号,'得分率',学科,convert(varchar,round(总分/".$maxScore."*100,1))+'%'".$fileds2."
		from 各题得分".$grade." where 学生ID=0 and 考试ID=".$examId." and 学科='".$subject."'
		order by 实验班,班号,名称 desc";
		$result = Db::query($sql);
		$result = selectResult($result);
		//excel表头内容
		$list = explode(",", $header);
		$headers = [];
		foreach ($list as $key=> $value){
			$headers[$value] = $value;
		}
		Excel::toExcel($result,'班级各题人平分',$headers);
		exit;
	}

}
